<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL操作数据库</title>
</head>
<body>
<!-- 1.设置数据源 :JDBC驱动名以及数据库，url,用户名，密码-->
<sql:setDataSource var="snapshot" driver="com.mysql.cj.jdbc.Driver" 
     url="jdbc:mysql://localhost:3306/niit?serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=utf8"
     user="root" password="root" />

<h1>1. 查询</h1>
<sql:query var="result" dataSource="${snapshot}">
 select * from book
</sql:query>

<table border="1">
	<tr>
		<th>序号</th>
		<th>编号</th>
		<th>名称</th>
		<th>作者</th>
		<th>单价</th>
	</tr>
	<c:forEach items="${result.rows}" var="bookItem">
		<tr>
			<td>${bookItem.id}</td>
			<td>${bookItem.code}</td>
			<td>${bookItem.name}</td>
			<td>${bookItem.author}</td>
			<td>${bookItem.unit_price}</td>
		</tr>
	</c:forEach>
</table>

<h1>2. 更新</h1>
<sql:update dataSource="${snapshot}" var="i">
 insert into book (code,name,author,unit_price) values ('book05','精通JSP','小红',88)
</sql:update>
<c:if test="${i == 1}">
	新增数据成功
</c:if>
<c:if test="${i != 1}">
	新增数据失败
</c:if>
</body>
</html>